"""
@summary: Extracts the genotypes from the vcf file

@author: charles.hefer@gmail.com
@version 0.1
"""
import optparse
import sys
import vcf

def stop_err( msg ):
	sys.stderr.write( "%s\n" % msg )
	sys.exit()
 
def __main__():
	#Parse Command Line
	parser = optparse.OptionParser()
	parser.add_option("-i", "--input", default=None, dest="input", 
					  help="The input vcf file")
	parser.add_option("-o", "--output", default=None, dest="output",
					  help="The output genotypes file")
	(options, args) = parser.parse_args()
	
	handle = open(options.input, "r")
	
	outfile = open(options.output, "w")
	outfile.write("Chrom\tPosition\n")
	vcf_lines = vcf.Reader(handle)
	for record in vcf_lines:
		line = None
		for sample in record.samples:
			if sample['GT'] == "0/0":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.REF[0]), str(record.REF[0])])
			elif sample['GT'] == "0/1":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.REF[0]), str(record.ALT[0])])
			elif sample['GT'] == "0/2":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.REF[0]), str(record.ALT[1])])
			elif sample['GT'] == "0/3":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.REF[0]), str(record.ALT[2])])
			elif sample['GT'] == "1/0":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[0]), str(record.REF[0])])
			elif sample['GT'] == "1/1":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[0]), str(record.ALT[0])])
			elif sample['GT'] == "1/2":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[0]), str(record.ALT[1])])
			elif sample['GT'] == "1/3":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[0]), str(record.ALT[2])])
			elif sample['GT'] == "2/1":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[1]), str(record.ALT[0])])
			elif sample['GT'] == "2/2":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[1]), str(record.ALT[1])])
			elif sample['GT'] == "2/3":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[1]), str(record.ALT[2])])
			elif sample['GT'] == "3/0":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[2]), str(record.REF[0])])
			elif sample['GT'] == "3/1":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[2]), str(record.ALT[0])])
			elif sample['GT'] == "3/2":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[2]), str(record.ALT[1])])
			elif sample['GT'] == "3/3":
				line = "\t".join([sample.sample, record.CHROM, str(record.end), str(record.ALT[2]), str(record.ALT[2])])
			elif sample['GT'] is None: 
				line = ""
			else:
				print sample['GT'], "An unknown GT field...."
				raise sys.exit("\t".join(sample.sample, sample["GT"]))
			outfile.write(line + "\n")
	outfile.close()
	
if __name__=="__main__": 
	__main__()
